package com.buka.book.dao;

import com.buka.book.entity.Book;
import com.buka.utils.JDBCUtil;

import java.util.List;

public class BookDao {

    public List<Book> bookList() {
        String sql = "select * from t_book";
        return JDBCUtil.executeQuery(sql, Book.class);
    }


    //根据id查询要修改的记录（原数据）
    public Book queryBookById(int id) {
        String sql = "select * from t_book where id = ?";
        List<Book> bookList = JDBCUtil.executeQuery(sql, Book.class, id);
        return bookList.get(0);

    }

    //修改数据库记录
    public int updateBook(Book book) {
        String sql = "update t_book set bookName = ?,count = ?,createTime = ?  where id = ?";
        return JDBCUtil.executeUpdateByArr(sql, book.getBookName(), book.getCount(), book.getCreateTime(), book.getId());

    }


    public Book queryBookByNo(String bookNo) {
        String sql = "select * from t_book where bookNo = ?";
        List<Book> bookList = JDBCUtil.executeQuery(sql, Book.class, bookNo);
        return bookList.isEmpty() ? null : bookList.get(0);
    }

    public int add(Book book) {

        String sql = "insert into t_book(bookName,bookNo,count,createTime) values(?,?,?,?)";
        return JDBCUtil.executeUpdateByArr(sql, book.getBookName(), book.getBookNo(),
                book.getCount(), book.getCreateTime());

    }
}
